//
//  LJYaoRecommendViewController.m
//  WWLNetease
//
//  Created by 李锦耀 on 15/12/22.
//  Copyright © 2015年 Echo.Jobs. All rights reserved.
//

#import "LJYaoRecommendViewController.h"
#import "ReadViewController.h"
#import "LJYRecDetailViewController.h"
#import "LJYRecommendModel.h"
#import "LJYRecommendmodelTwo.h"
#import "LJYMyTableViewCell.h"
#import "LJYMyTableViewCellTwo.h"
#import "AppTools.h"
#import "MJRefresh.h"

@interface LJYaoRecommendViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, strong)UITableView *myTableView;
@property(nonatomic, strong)NSMutableArray *recommendArray;
@property(nonatomic, assign)BOOL isUpLoading;
@property(nonatomic, copy)NSString *urlStr;
@property(nonatomic, strong)ReadViewController *read;

@end

@implementation LJYaoRecommendViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    _recommendArray = [NSMutableArray array];
    self.view.backgroundColor = [UIColor whiteColor];
    self.isUpLoading = NO;
    [self creatTableView];
    
}



- (void)upTableView
{
    self.urlStr = @"http://c.3g.163.com/recommend/getSubDocPic?passport=&devId=866479021081277&size=20&version=5.3.6&from=yuedu&net=wifi";
    
    [self addData: self.urlStr andDwon:NO];
}

#pragma mark - 上拉刷新
- (void)downTableView{
    
    self.urlStr = @"http://c.3g.163.com/recommend/getSubDocPic?passport=&devId=866479021081277&size=20&version=5.3.6&from=yuedu&net=wifi";
    
    [self addData:self.urlStr andDwon:YES];
}

- (void)addData:(NSString *)url andDwon:(BOOL)isDown
{
    [AppTools getDataWithURL:url Block:^(id result) {
        
        if (!isDown)
        {
            [self.recommendArray removeAllObjects];
        }
        NSDictionary *dictionary = result;
        NSArray *array = dictionary[@"推荐"];
        for (NSDictionary *dic in array)
        {
            if ([[dic allKeys] containsObject:@"imgnewextra"])
            {
                LJYRecommendmodelTwo *model = [[LJYRecommendmodelTwo alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [self.recommendArray addObject:model];
            }
            else
            {
                LJYRecommendModel *model = [[LJYRecommendModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [self.recommendArray addObject:model];
            }
        }
        [self.myTableView.mj_header endRefreshing];
        [self.myTableView.mj_footer endRefreshing];

        [self.myTableView reloadData];
        
    } ErrorBlock:^(NSError *error) {
        
        NSLog(@"error:%@", error);
    }];
    

}

-(void)creatTableView
{
    _myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height-100) style:UITableViewStyleGrouped];
    [self.view addSubview:_myTableView];
    _myTableView.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1];
    _myTableView.delegate = self;
    _myTableView.dataSource = self;
    [_myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.bottom.mas_equalTo(-100);
        make.top.equalTo(self.view);;
        make.left.equalTo(self.view);
        make.right.equalTo(self.view);
    }];
    self.myTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(upTableView)];
    [self.myTableView.mj_header beginRefreshing];
    
    self.myTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(downTableView)];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.recommendArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 4;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 4;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([[self.recommendArray objectAtIndex:indexPath.section] isKindOfClass:[LJYRecommendModel class]])
    {
        return 110;
    }
    else
    {
        return 210;
    }

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([[self.recommendArray objectAtIndex:indexPath.section] isKindOfClass:[LJYRecommendmodelTwo class]])
    {
        static NSString *cellInditifyTwo = @"cellTwo";
        LJYMyTableViewCellTwo *cell = [tableView dequeueReusableCellWithIdentifier:cellInditifyTwo];
        if (cell == nil)
        {
            cell = [[LJYMyTableViewCellTwo alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellTwo"];
        }
        cell.recommend = [self.recommendArray objectAtIndex:indexPath.section];
        return cell;
    }
    static NSString *cellInditify = @"cell";
    LJYMyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellInditify];
    if (cell == nil)
    {
        cell = [[LJYMyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.recommend = [self.recommendArray objectAtIndex:indexPath.section];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.myTableView deselectRowAtIndexPath:indexPath animated:YES];
     LJYRecDetailViewController *detail = [[LJYRecDetailViewController alloc] init];
    if ([[self.recommendArray objectAtIndex:indexPath.section] isKindOfClass:[LJYRecommendModel class]])
    {
        LJYRecommendModel *model = [self.recommendArray objectAtIndex:indexPath.section];
        detail.loadId = model.docid;
    }
    else
    {
        LJYRecommendmodelTwo *model = [self.recommendArray objectAtIndex:indexPath.section];
        detail.loadId = model.docid;
    }

    [[NSNotificationCenter defaultCenter] postNotificationName:@"changeFrame" object:nil];
    [self.navigationController pushViewController:detail animated:YES];
    self.tabBarController.tabBar.hidden = YES;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"changeFrameBack" object:nil];
     self.tabBarController.tabBar.hidden = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
